<?php

namespace services\models;

use Yii;

/**
 * This is the model class for table "d_try.order_goods".
 *
 * @property integer   $order_goods_id
 * @property integer   $order_info_id
 * @property integer   $goods_id
 * @property integer   $product_id
 * @property integer   $bestry_uid
 * @property integer   $buy_number
 * @property double    $buy_price
 * @property string    $create_time
 * @property integer   $free_gift
 * @property integer   $commented
 * @property integer   $is_comment
 * @property integer   $activity_type_id
 * @property integer   $activity_id
 * @property integer   $price_type
 * @property integer   $try_coin
 *
 * @property OrderInfo $orderInfo
 */
class OrderGoods extends \common\models\OrderGoods
{
    public static $_GIFT_NO  = 0;
    public static $_GIFT_YES = 1;
    public static $GIFT_DESC = ['无', '有'];


    public static $_COMMENTED_HAVE     = 1;
    public static $_COMMENTED_HAVE_NOT = 0;
    public static $COMMENTED_DESC      = ['无', '有'];


    public $goodsName = '';
    public $redis;

    public function init()
    {
        $this->redis = Yii::$app->redis_goods;
        parent::init();
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->create_time = date('Y-m-d H:i:s');
        }
        return parent::beforeSave($insert);
    }


    public function afterSave($insert, $changedAttributes)
    {
        if ($insert && $this->beforeSave($insert)) {
            /** @var ProductStock $productStock */
            /*$productStock = ProductStock::findOne(['product_id' => $this->product_id]);
            if ($productStock->stock < $this->buy_number) {
                $this->addError('buy_number', $this->goodsName . '库存不足请重新选择');
            }
            $productStock->stock -= $this->buy_number;
            $productStock->save();*/
            ProductStock::updateAllCounters(['stock' => -$this->buy_number, 'sale_num' => $this->buy_number], ['product_id' => $this->product_id]);
            $stock = ProductStock::find()->where(['product_id' => $this->product_id])->select(['stock'])->scalar();
            if ($stock < 0) {
                $this->addError($this->product_id, $this->goodsName . '库存不足请重新选择');
            }

        }
        return parent::afterSave($insert, $changedAttributes);
    }

    //关系 order_info == order_express
    public function getOrderInfo()
    {
        return $this->hasOne(GoodsDetail::className(), ['goods_id' => 'goods_id']);
    }


    public function getProductDetail()
    {
        return $this->hasOne(ProductDetail::className(), ['product_id' => 'produt_id']);
    }

    public function getProduct()
    {
        return $this->hasOne(Product::className(), ['product_id' => 'product_id']);
    }

    public function getGoodsDetail()
    {
        return $this->hasOne(GoodsDetail::className(), ['goods_id' => 'goods_id']);
    }

    public function getGoodsName()
    {
        $key       = 'goods_name';
        $goodsName = Yii::$app->redis_goods->hget($key, $this->goods_id);
        if (empty($goodsName)) {
            /** @var GoodsDetail $gd */
            $gd = GoodsDetail::findByGoodsId($this->goods_id);
            Yii::$app->redis_goods->hset($key, $this->goods_id, $gd->goods_name);
            return $gd->goods_name;
        }
        return $goodsName;
    }

    public function getProductSpec()
    {
        $key  = 'product_spec';
        $spec = Yii::$app->redis_goods->hget($key, $this->goods_id);
        if (empty($spec)) {
            /** @var ProductDetail $pd */
            $pd = ProductDetail::findOne(['product_id' => $this->product_id]);
            Yii::$app->redis_goods->hset($key, $this->product_id, $pd->spec_desc);
            return $pd->spec_desc;
        }
        return $spec;
    }

}
